package cpa.prowon.gui;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;

import cpa.prowon.Application;
import cpa.prowon.gui.util.ColorManager;
import cpa.prowon.image.ImageContainer;
import cpa.prowon.image.ThumbnailManager;

/**
 * Represents the preview part
 * @author Chris
 */
public class WindowPreviewPart extends WindowPart {

	private ImageContainer selected_image = null;
	private Label iptc_title;
	private Label iptc_gallery;
	private Label iptc_city;
	private Label iptc_state;
	private Label iptc_country;
	private Label iptc_category;
	private Label iptc_copyright;
	private Label iptc_date;
	private Label file_value_label;
	private Canvas canvas;
	
	/**
	 * Handles the paint listener
	 * @author Chris
	 */
	private class CanvasPaintListener implements PaintListener
	{
		public void paintControl(final PaintEvent e)
		{
			if (selected_image != null) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						Image _image = ThumbnailManager.getInstance().getThumbnail(selected_image);
						if (_image != null) {
							int maxSize = (new Integer(Application.getInstance().getApplicationProperties().getProperty(MainWindow.PROPNAME_THUMBNAILSIZE))).intValue();
							int x = (maxSize - _image.getImageData().width) / 2;
							int y = (maxSize - _image.getImageData().height) / 2;
							e.gc.drawImage(_image, x, y);
						}
					}
				});
			} else {
				e.gc.dispose();
			}
		}
	}
	
	/**
	 * Set the selected image
	 * @param ImageContainer The selected image
	 */
	public void setSelectedImage(ImageContainer anImage)
	{
		
		selected_image = anImage;
		
		if (selected_image == null) {
			canvas.redraw();
		} else {
			ThumbnailManager.getInstance().createThumbnail(selected_image, getCanvas());
		}
		setPictureInfo(selected_image);
		
	}

	/**
	 * Constructor
	 * @param MainWindow The main window
	 */
	public WindowPreviewPart(MainWindow aMainWindow) {
		super(aMainWindow);
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#getTitle()
	 */
	protected String getTitle() {
		return Application.getInstance().getResourceBundle().getString("title_picture_details");
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#getIcon()
	 */
	protected String getIcon() {
		return "icons/picture.gif";
	}

	/* (non-Javadoc)
	 * @see cpa.prowon.gui.WindowPart#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected void createContents(Composite parent) {

		// Create the canvas
		GridLayout parentGrid = new GridLayout();
		parentGrid.numColumns = 3;
		parent.setLayout(parentGrid);
		
		canvas = new Canvas(parent, SWT.BORDER);
		canvas.setBackground(ColorManager.getInstance().getColor(SWT.COLOR_WHITE));
		GridData gridData = new GridData();
		int maxSize = (new Integer(Application.getInstance().getApplicationProperties().getProperty(MainWindow.PROPNAME_THUMBNAILSIZE))).intValue();
		gridData.widthHint = maxSize;
		gridData.heightHint = maxSize;
		gridData.verticalAlignment = GridData.BEGINNING;
		gridData.verticalSpan = 10;
		canvas.setLayoutData(gridData);
		GridLayout canvas_layout = new GridLayout();
		canvas_layout.horizontalSpacing = GridData.HORIZONTAL_ALIGN_CENTER;
		canvas_layout.verticalSpacing = GridData.VERTICAL_ALIGN_CENTER;
		canvas.setLayout(canvas_layout);
		
		// Add the event listenter
		canvas.addPaintListener(new CanvasPaintListener());
		
		// Create the details information
		ResourceBundle rb = Application.getInstance().getResourceBundle();
		iptc_title = createIPTCLabel(parent, "title");
		iptc_gallery = createIPTCLabel(parent, "gallery");
		iptc_city = createIPTCLabel(parent, "city");
		iptc_state = createIPTCLabel(parent, "state");
		iptc_country = createIPTCLabel(parent, "country");
		iptc_category = createIPTCLabel(parent, "category");
		iptc_copyright = createIPTCLabel(parent, "copyright");
		iptc_date = createIPTCLabel(parent, "date");

		// Create the file label
		Label file_label = new Label(parent, 1);
		file_label.setText(Application.getInstance().getResourceBundle().getString("imageinfo_file") + ":");
		file_label.setBackground(ColorManager.getInstance().getColor(SWT.COLOR_WHITE));
		
		// Create the value label
		file_value_label = new Label(parent, 1);
		GridData label_grid = new GridData(GridData.FILL_HORIZONTAL);
		file_value_label.setData(label_grid);
		file_value_label.setBackground(ColorManager.getInstance().getColor(SWT.COLOR_WHITE));

	}

	/**
	 * Create an IPTC label
	 * @param Composite The parent where the label should be put into
	 * @param String The tag to be used
	 * @return The IPTC value label
	 * @author Chris
	 */
	protected Label createIPTCLabel(Composite parent, String tag) {
		
		// Create the label
		Label iptc_label = new Label(parent, 1);
		iptc_label.setText(Application.getInstance().getResourceBundle().getString("imageinfo_" + tag) + ":");
		iptc_label.setBackground(ColorManager.getInstance().getColor(SWT.COLOR_WHITE));
		
		// Create the value label
		Label iptc_value_label = new Label(parent, 1);
		GridData label_grid = new GridData(GridData.FILL_HORIZONTAL);
		iptc_value_label.setData(label_grid);
		iptc_value_label.setBackground(ColorManager.getInstance().getColor(SWT.COLOR_WHITE));
		
		// Return the label
		return iptc_value_label;
		
	}
	
	/**
	 * Set the picture information
	 * @param ImageContainer The image container that is currently selected
	 * @author Chris
	 */
	public void setPictureInfo(ImageContainer ic) {
		if (ic != null) {
			iptc_title.setText(ic.getTitle());
			iptc_gallery.setText(ic.getGallery());
			iptc_city.setText(ic.getCity());
			iptc_state.setText(ic.getProvinceState());
			iptc_country.setText(ic.getCountry());
			iptc_category.setText(ic.getCategory());
			iptc_copyright.setText(ic.getCopyright());
			iptc_date.setText(ic.getDate());
			file_value_label.setText(ic.getPathName());
		} else {
			iptc_title.setText("");
			iptc_gallery.setText("");
			iptc_city.setText("");
			iptc_state.setText("");
			iptc_country.setText("");
			iptc_category.setText("");
			iptc_copyright.setText("");
			iptc_date.setText("");
			file_value_label.setText("");
		}
		iptc_title.pack();
		iptc_gallery.pack();
		iptc_city.pack();
		iptc_state.pack();
		iptc_country.pack();
		iptc_category.pack();
		iptc_copyright.pack();
		iptc_date.pack();
		file_value_label.pack();
	}
	
	/**
	 * Return the canvas
	 * @return The canvas
	 * @author Chris
	 */
	public Canvas getCanvas()
	{
		return canvas;
	}

}
